using UnityEngine;
using System.Collections;

public class MoveCamera : MonoBehaviour {
	
	//
	// VARIABLES
	//
	
	public float turnSpeed;				// Speed of camera turning when mouse moves in along an axis
	public float dragSpeed;				// Speed of the camera when being dragged
	public float zoomSpeed;				// Speed of the camera going back and forth
	
	private Vector3 dragOrigin;			// Position of cursor when drag starts
	private bool isDragging;			// Is the camera being dragged?
	private bool isRotating;			// Is the camera being rotated?
	private bool isZooming;				// Is the camera zooming?
	
	private bool alreadyCheckMouseMovement = false;
	//
	// START
	//
	
	void Start () 
	{
		
	}
	
	//
	// UPDATE
	//
	
	void Update () 
	{
		// Get the left mouse button
		if(Input.GetMouseButtonDown(0))
		{
			// Get drag origin
			dragOrigin = Input.mousePosition;
			isRotating = true;
		}
		
		// Get the right mouse button
		if(Input.GetMouseButtonDown(1))
		{
			// Get drag origin
			dragOrigin = Input.mousePosition;
			isDragging = true;
		}
		
		// Get the middle mouse button
		if(Input.GetMouseButtonDown(2))
		{
			// Get drag origin
			dragOrigin = Input.mousePosition;
			isZooming = true;
		}
		
		// Disable movements on button release
		if (!Input.GetMouseButton(0)) 
		{
			isRotating=false;
			alreadyCheckMouseMovement = false;
			SelectObjectByClick.wasRotatingCam = false;
		}
		
		if (!Input.GetMouseButton(1))
		{
			isDragging=false;
		}
		
		if (!Input.GetMouseButton(2)) 
		{
			isZooming=false;
		}
		
		// Rotate camera along X and Y axis
		if (isRotating)
		{
	        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
			
			// Check if mouse moved for camera rotation
			// Used to determine if an object was being selected or if you were rotating the camera
			if((dragOrigin != Input.mousePosition) && !alreadyCheckMouseMovement)
			{
				alreadyCheckMouseMovement = true;
				SelectObjectByClick.wasRotatingCam = true;
			}
			
			transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed);
			transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed);
		}
		
		// Move the camera on it's XY plane
		if (isDragging)
		{
	        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
	        Vector3 move = new Vector3(pos.x * dragSpeed, pos.y * dragSpeed, 0);
	
	        transform.Translate(move, Space.Self);
		}
		
		// Move the camera linearly along Z axis
		if (isZooming)
		{
	        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
	        Vector3 move = pos.y * zoomSpeed * transform.forward; 
	
	        transform.Translate(move, Space.World);
		}
	}
}

/*
  // CREDIT: damien_oconnell  
  
using UnityEngine;

public class CameraDrag : MonoBehaviour
{
    public float dragSpeed = 2;
    private Vector3 dragOrigin;


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            dragOrigin = Input.mousePosition;
            return;
        }

        if (!Input.GetMouseButton(0)) return;

        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
        Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);

        transform.Translate(move, Space.World);  
    }
}
 */
